import time

import allure
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from page.base_page import BasePage


class AddHistoryOrder(BasePage):
    __ORDER_STATUS = By.XPATH, "//*[text()='请选择订单状态']"
    __FILE_MASK = By.XPATH, "//*[@class = 'el-loading-spinner']"
    __ElE_MASK = By.XPATH, "//*[@class = 'el-loading-mask']"
    __CAPITAL_COMPANY = By.XPATH, "//*[@class= 'el-row scroll-item addrow']//*[text()='请选择资金方']"
    __PRODUCT_TYPE = By.XPATH, "//*[@class= 'el-row scroll-item addrow']//*[text()='请选择产品类型']"
    __PRODUCT_NAME = By.XPATH, "//*[@class= 'el-row scroll-item addrow']//*[text()='请选择产品']"
    __BORROWER_TYPE_INDIVIDUAL = By.XPATH, "//*[text()='主借人信息 ']/../..//*[text()='个人']"
    __BORROWER_TYPE_CORPORATE = By.XPATH, "//*[text()='主借人信息 ']/../..//*[text()='对公']"
    __UPLOAD_IDCARD = By.XPATH, "//*[@class = 'el-upload el-upload--text is-drag']/input"
    __UPLOAD_FILE = By.XPATH, "//*[@class = 'el-upload__input']"
    __NAME = By.XPATH, "//*[@placeholder = '请选择客户']"
    __PHONE = By.XPATH, "//*[@placeholder = '请输入手机号码']"
    __SSN = By.XPATH, "//*[@placeholder = '请输入身份证']"
    __START_DATE = By.XPATH, "//*[@placeholder = '身份证有效起始日期']"
    __END_DATE = By.XPATH, "//*[@placeholder = '身份证有效结束日期']"
    __ID_ADDRESS = By.XPATH, "//*[@placeholder = '请输入身份证地址']"
    __BIRTH_DATE = By.XPATH, "//*[@placeholder = '请选择出生日期']"
    __DURATION = By.XPATH, "//*[text() = '请选择现居地居住年限']"
    __MARRIAGE = By.XPATH, "//*[text() = '请选择婚姻情况']"
    __QUALIFICATION = By.XPATH, "//*[text() = '请选择目前学历']"
    __RESIDENTIAL = By.XPATH, "//*[text()='现居地']/../div/div/div/div"
    __ADDRESS = By.XPATH, "//*[@placeholder = '请输入详细地址']"
    __CORPORATE_NAME = By.XPATH, "//*[@placeholder = '请输入公司名称']"
    __COMPANY_CREDIT_CODE = By.XPATH, "//*[text() = '公司信用代码：']/../div//input"
    __COMPANY_ADDRESS = By.XPATH, "//*[@placeholder='请选择']"
    __DETAILED_ADDRESS = By.XPATH, "//*[text() = '公司地址：']/../div[2]/div[2]//input"
    __ID_TYPE = By.XPATH, "//*[text() = '请选择证件类型']"
    __LEGAL_PERSON_NAME = By.XPATH, "//*[text() = '法人代表人姓名']/../div//input"
    __LEGAL_PERSON_SSN = By.XPATH, "//*[text() = '证件号码：']/../div//input"
    __LEGAL_PERSON_PHONE = By.XPATH, "//*[text() = '手机号']/../div//input"
    __LOAN_AMOUNT = By.XPATH, "//*[@placeholder = '请输入借款金额']"
    __LOAN_PERIOD = By.XPATH, "//*[@placeholder = '请输入借款期数']"
    __REPAYMENT_METHOD = By.XPATH, "//*[text() = '请选择还款方式']"
    __LOAN_PURPOSE = By.XPATH, "//*[@placeholder = '请输入借贷用途']"
    __BANK_ACCOUNT_NAME = By.XPATH, "//*[@placeholder = '请输入账户名']"
    __OPENING_BANK = By.XPATH, "//*[text()='开户行']/../div/div/div"
    __BANKCARD_NUMBER = By.XPATH, "//*[@placeholder = '请输入收款账户银行卡号']"
    __BANK_PHONE = By.XPATH, "//*[@placeholder = '请输入预留手机号']"
    __SAVE_BUTTON = By.XPATH, "//*[text() = '保存借贷信息']"
    __SUBMIT_BUTTON = By.XPATH, "//*[text() = '确定提交订单']"
    __CANCEL_BUTTON = By.XPATH, "//*[text() = '取 消']"
    __ADD_CO_BORROWER = By.XPATH, "//*[text() = '+ 添加共借人']"
    __CORPORATE_IDCARD_UPLOAD = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[@class = 'el-upload__input']"
    __CO_BORROWER_NAME = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text() = '姓名']/../div/div/div/input"
    __CO_BORROWER_SSN = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text() = '身份证']/../div/div/div/input"
    __CO_BORROWER_IDCARD_ADDRESS = (By.XPATH,
                                    "//*[@class='el-dialog is-draggable a_order']//*[text() = "
                                    "'身份证地址']/../div/div/div/input")
    __CO_BORROWER_IDCARD_START_DATE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[@placeholder='起始日期']"
    __CO_BORROWER_IDCARD_END_DATE = (By.XPATH,
                                     "//*[@class='el-dialog is-draggable a_order']//*[@placeholder='结束日期']")
    __CO_BORROWER_PHONE = By.XPATH, ("//*[@class='el-dialog is-draggable a_order']//*[text() = "
                                     "'手机号']/../div/div/div/input")
    __CO_BORROWER_ADDRESS = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[@placeholder='请选择']"
    __CO_BORROWER_DETAIL_ADDRESS = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[@placeholder='请输入详细地址']"
    __CO_BORROWER_RELATIONSHIP = By.XPATH, ("//*[@class='el-dialog is-draggable a_order']"
                                            "//*[text() = '与主借人关系']/../div/div/div")
    __CO_BORROWER_ADD_FILE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text()='添加文件']"
    __CO_BORROWER_FILE_TYPE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text()='请选择附件分类']"
    __CO_BORROWER_SELECT_FILE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text()='选择文件']"
    __CO_BORROWER_UPLOAD = By.XPATH, "//*[@aria-label = '上传文件']//input"
    __CO_BORROWER_FILE_NAME = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//tbody/tr/td[3]/div"
    __CO_BORROWER_DOWNLOAD_FILE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text()='下载']"
    __CO_BORROWER_DELETE_FILE = By.XPATH, "//*[@class='el-dialog is-draggable a_order']//*[text()='删除']"
    __CO_BORROWER_EDIT = By.XPATH, "//*[text()='担保人信息 ']/../..//tbody//*[text()='编辑']"
    __CO_BORROWER_DELETE = By.XPATH, "//*[text()='担保人信息 ']/../..//tbody//*[text()='删除']"
    __ADD_OK_BUTTON = By.XPATH, "//*[@class = 'el-button el-button--primary el-button--default']//*[text()='确定']"
    __FIRST_INFO_NAME = By.XPATH, "//*[@class='el-dialog__title']/../..//*[@class = 'el-table__row'][1]/td[3]/div"
    __ADD_GUARANTOR = By.XPATH, "//*[text() = '+ 添加担保人']"
    __ADD_COLLATERAL = By.XPATH, "//*[text() = '+ 添加抵押物']"
    __MODIFY_COLLATERAL = By.XPATH, "//*[text() = '抵押信息 ']/../..//*[@class = 'el-table__row']//*[text()='编辑']"
    __PROPERTY_OWNERSHIP_CERTIFICATE = By.XPATH, "//*[text()='房产证照片']/../..//input"
    __PROPERTY_OWNER_IDCARD = By.XPATH, "//*[text()='产权人身份证']/../div/div/div/input"
    __PROPERTY_OWNER_NAME = By.XPATH, "//*[text()='产权人姓名']/../div/div/div/input"
    __PROPERTY_OWNER_PHONE = By.XPATH, "//*[text()='产权人手机号码']/../div/div/div/input"
    __PROPERTY_OWNER_ADDRESS = By.XPATH, "//*[text()='产权人地址']/../div/div/div/input"
    __OWNERSHIP_CERTIFICATE = By.XPATH, "//*[text()='权属证书编号']/../div/div/div/input"
    __PROPERTY_ADDRESS = By.XPATH, "//*[text()='抵押物地址']/../div/div/div/input"
    __BUILDING_AREA = By.XPATH, "//*[text()='建筑面积']/../div/div/div/input"
    __EVALUATE_VALUE = By.XPATH, "//*[text()='评估价值']/../div/div/div/input"
    __MARITAL_STATUS = By.XPATH, "//*[text()='婚姻状态']/../div/div/div"
    __BELONG_REGION = By.XPATH, "//*[text()='所属区域']/../div/div/div/input"
    __POWER_VALUE = By.XPATH, "//*[text()='权力价值']/../div/div/div/input"
    __MORTGAGE_RATE = By.XPATH, "//*[text()='抵押率']/../div/div/div/input"
    __ADD_COLLATERAL_OK = By.XPATH, "//*[@class = 'el-button el-button--primary el-button--default']//*[text()='确定']"
    __COLLATERAL_FIRST_INFO_NAME = By.XPATH, "//*[text() = '抵押信息 ']/../..//*[@class = 'el-table__row']/td[2]"
    __DELETE_FIRST_COLLATERAL = By.XPATH, "//*[text() = '抵押信息 ']/../..//*[@class = 'el-table__row']//*[text()='删除']"
    __DELETE_SECOND_OK = By.XPATH, "//*[@aria-label ='系统提示']//*[text()='确定']"
    __MESSAGE = By.XPATH, "//*[@class = 'el-message__content']"
    __DELETE_SUCCESSFULLY_MESSAGE = By.XPATH, "//*[text() = '删除成功！']"
    __BORROWING_TIME = By.XPATH, "//*[text() = '借贷信息 ']/../..//*[@placeholder ='借款日期']"
    __REPAYMENT_TIME = By.XPATH, "//*[text() = '借贷信息 ']/../..//*[@placeholder ='结束日期']"
    __REPAYMENT_EVERY_DATE = By.XPATH, "//*[@placeholder ='请选择每期还款日']"
    __INTEREST_RATE = By.XPATH, "//*[@placeholder ='请输入利率']"
    __TOTAL_INTEREST = By.XPATH, "//*[@placeholder ='请输入总利息']"
    __COMMISSION = By.XPATH, "//*[@placeholder ='请输入手续费']"

    def select_order_status(self, order_status):
        with allure.step(f"选择订单状态：{order_status}"):
            self.find_and_click(self.__ORDER_STATUS)
            self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{order_status}']")
        return AddHistoryOrder(self.driver)

    def select_capital_info(self, capital_company, product_type, product_name):
        self.find_and_click(self.__CAPITAL_COMPANY)
        self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{capital_company}']")
        self.find_and_click(self.__PRODUCT_TYPE)
        self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{product_type}']")
        self.find_and_click(self.__PRODUCT_NAME)
        self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{product_name}']")
        return AddHistoryOrder(self.driver)

    def input_borrower_info(self, borrower_type="个人", idcard_front=None, idcard_back=None, name=None, gender=None,
                            phone=None, ssn=None, start_date=None, end_date=None, id_address=None, birth_date=None,
                            domicile=None, duration=None, marriage=None, qualification=None, residential=None,
                            address=None, business_license=None, corporate_name=None, company_credit_code=None,
                            company_address=None, detailed_address=None, id_type=None, legal_person_name=None,
                            legal_person_ssn=None, legal_person_phone=None):
        if borrower_type == "个人":
            with allure.step("选择借贷人类型为个人"):
                self.find_and_click(self.__BORROWER_TYPE_INDIVIDUAL)
            if idcard_front is not None and idcard_back is not None:
                ele = self.finds(self.__UPLOAD_IDCARD)
                with allure.step("上传身份证正面"):
                    ele[0].send_keys(idcard_front)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
                with allure.step("上传身份证反面"):
                    ele[1].send_keys(idcard_back)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
            if name is not None:
                with allure.step(f"输入姓名为{name}"):
                    self.find(self.__NAME).clear()
                    self.find_and_send(self.__NAME, text=name)
            if gender is not None:
                with allure.step(f"选择性别为{gender}"):
                    self.find_and_click(By.XPATH, f"//*[@class = 'el-radio-group ml-4']//*[text()='{gender}']")
            if phone is not None:
                with allure.step(f"输入手机号为{phone}"):
                    self.find_and_send(self.__PHONE, text=phone)
            if ssn is not None:
                with allure.step(f"输入身份证号为{ssn}"):
                    self.find(self.__SSN).clear()
                    self.find_and_send(self.__SSN, text=ssn)
            if start_date is not None:
                with allure.step(f"选择身份证有效日期为{start_date}"):
                    self.find(self.__START_DATE).clear()
                    self.find_and_send(self.__START_DATE, text=start_date)
            if end_date is not None:
                with allure.step(f"选择身份证有效结束日期为{end_date}"):
                    self.find(self.__END_DATE).clear()
                    self.find_and_send(self.__END_DATE, text=end_date)
            if id_address is not None:
                with allure.step(f"输入身份证地址为{id_address}"):
                    self.find(self.__ID_ADDRESS).clear()
                    self.find_and_send(self.__ID_ADDRESS, text=id_address)
            if birth_date is not None:
                with allure.step(f"选择出生日期为{birth_date}"):
                    self.find(self.__BIRTH_DATE).clear()
                    self.find_and_send(self.__BIRTH_DATE, text=birth_date)
            if domicile is not None:
                with allure.step(f"选择户籍为{domicile}"):
                    self.find_and_click(By.XPATH, f"//*[@class = 'el-radio-group ml-4']//*[text()='{domicile}']")
            if duration is not None:
                with allure.step(f"选择现居地居住年限为{duration}"):
                    self.find_and_click(self.__DURATION)
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text()='{duration}']")
            if marriage is not None:
                with allure.step(f"选择婚姻情况为{marriage}"):
                    self.find_and_click(self.__MARRIAGE)
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{marriage}']")
            if qualification is not None:
                with allure.step(f"选择目前学历为{qualification}"):
                    self.find_and_click(self.__QUALIFICATION)
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{qualification}']")
            if residential is not None:
                with allure.step(f"选择现居住地为{residential}"):
                    self.find_and_click(self.__RESIDENTIAL)
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{residential[0]}']")
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{residential[1]}']")
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{residential[2]}']")
            if address is not None:
                with allure.step(f"输入详细地址为{address}"):
                    self.find_and_send(self.__ADDRESS, text=address)
        if borrower_type == "对公":
            with allure.step("选择借贷人类型为对公"):
                self.find_and_click(self.__BORROWER_TYPE_CORPORATE)
            if business_license is not None:
                ele = self.finds(self.__UPLOAD_FILE)
                with allure.step("上传营业执照"):
                    ele[0].send_keys(business_license)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=20)
                    time.sleep(0.2)
            if corporate_name is not None:
                with allure.step(f"输入姓名为{corporate_name}"):
                    self.find(self.__CORPORATE_NAME).clear()
                    self.find_and_send(self.__CORPORATE_NAME, text=corporate_name)
            if company_credit_code is not None:
                with allure.step(f"输入公司信用代码{company_credit_code}"):
                    self.find(self.__COMPANY_CREDIT_CODE).clear()
                    self.find_and_send(self.__COMPANY_CREDIT_CODE, text=company_credit_code)
            if company_address is not None:
                with allure.step(f"选择现居住地为{company_address}"):
                    self.find_and_click(self.__COMPANY_ADDRESS)
                    self.find_and_click(By.XPATH,
                                        f"//div[@aria-hidden = 'false']//span[text() = '{company_address[0]}']")
                    self.find_and_click(By.XPATH,
                                        f"//div[@aria-hidden = 'false']//span[text() = '{company_address[1]}']")
                    self.find_and_click(By.XPATH,
                                        f"//div[@aria-hidden = 'false']//span[text() = '{company_address[2]}']")
            if detailed_address is not None:
                with allure.step(f"输入详细地址为{detailed_address}"):
                    self.find(self.__DETAILED_ADDRESS).clear()
                    self.find_and_send(self.__DETAILED_ADDRESS, text=detailed_address)
            if id_type is not None:
                with allure.step(f"选择证件类型为{id_type}"):
                    self.find_and_click(self.__ID_TYPE)
                    self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text()='{id_type}']")
            if idcard_front is not None and idcard_back is not None:
                ele = self.finds(self.__UPLOAD_FILE)
                with allure.step("上传身份证正面"):
                    ele[1].send_keys(idcard_front)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
                with allure.step("上传身份证反面"):
                    ele[2].send_keys(idcard_back)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
            if legal_person_name is not None:
                with allure.step(f"输入法人姓名为{legal_person_name}"):
                    self.find(self.__LEGAL_PERSON_NAME).clear()
                    self.find_and_send(self.__LEGAL_PERSON_NAME, text=legal_person_name)
            if legal_person_ssn is not None:
                with allure.step(f"输入法人身份证号为{legal_person_ssn}"):
                    self.find(self.__LEGAL_PERSON_SSN).clear()
                    self.find_and_send(self.__LEGAL_PERSON_SSN, text=legal_person_ssn)
            if legal_person_phone is not None:
                with allure.step(f"输入法人手机号为{legal_person_phone}"):
                    self.find_and_send(self.__LEGAL_PERSON_PHONE, text=legal_person_phone)
        return AddHistoryOrder(self.driver)

    def input_borrow_info(self, loan_amount=None, loan_period=None, repayment_method=None, borrowing_time=None,
                          repayment_time=None, repayment_every_date=None, interest_rate =None, total_interest=None,
                          commission=None, loan_purpose=None, bank_account_name=None, opening_bank=None,
                          bankcard_number=None, bank_phone=None):
        if loan_amount is not None:
            with allure.step(f"输入借款金额{loan_amount}"):
                self.find_and_send(self.__LOAN_AMOUNT, text=loan_amount)
        if loan_period is not None:
            with allure.step(f"输入借款期数{loan_period}"):
                self.find_and_send(self.__LOAN_PERIOD, text=loan_period)
        if repayment_method is not None:
            with allure.step(f"选择还款方式为{repayment_method}"):
                self.find_and_click(self.__REPAYMENT_METHOD)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{repayment_method}']")
        if borrowing_time is not None and repayment_time is not None:
            with allure.step(f"选择借款时间{borrowing_time}，还款时间为{repayment_time}"):
                self.find_and_send(self.__BORROWING_TIME, text=borrowing_time)
                self.find_and_send(self.__REPAYMENT_TIME, text=repayment_time)
        if repayment_every_date is not None:
            with allure.step(f"输入每期还款日为{repayment_every_date}"):
                self.find_and_send(self.__REPAYMENT_EVERY_DATE,text=repayment_every_date)
        if interest_rate is not None:
            with allure.step(f"输入利率为{interest_rate}"):
                self.find_and_send(self.__INTEREST_RATE, text=interest_rate)
        if total_interest is not None:
            with allure.step(f"输入总利息为{total_interest}"):
                self.find_and_send(self.__TOTAL_INTEREST, text=total_interest)
        if commission is not None:
            with allure.step(f"输入手续费为{commission}"):
                self.find_and_send(self.__COMMISSION, text=commission)
        if loan_purpose is not None:
            with allure.step(f"输入借贷用途为{loan_purpose}"):
                self.find_and_send(self.__LOAN_PURPOSE, text=loan_purpose)
        if bank_account_name is not None:
            with allure.step(f"输入账户名{bank_account_name}"):
                self.find_and_send(self.__BANK_ACCOUNT_NAME, text=bank_account_name)
        if opening_bank is not None:
            with allure.step(f"选择开户行{opening_bank}"):
                self.find_and_click(self.__OPENING_BANK)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{opening_bank}']")
        if bankcard_number is not None:
            with allure.step(f"输入银行卡号{bankcard_number}"):
                self.find(self.__BANKCARD_NUMBER).clear()
                self.find_and_send(self.__BANKCARD_NUMBER, text=bankcard_number)
        if bank_phone is not None:
            with allure.step(f"输入银行预留手机号{bank_phone}"):
                self.find_and_send(self.__BANK_PHONE, text=bank_phone)
        return AddHistoryOrder(self.driver)

    #
    def save_button(self):
        with allure.step("点击保存借贷信息"):
            self.find_and_click(self.__SAVE_BUTTON)
            return AddHistoryOrder(self.driver)

    #
    def submit_button(self):
        from page.history_order_page import HistoryOrder
        with allure.step("点击确定提交订单"):
            self.find_and_click(self.__SUBMIT_BUTTON)
            time.sleep(0.2)
            return HistoryOrder(self.driver)

    #
    def cancel_button(self):
        from page.history_order_page import HistoryOrder
        with allure.step("点击取消"):
            time.sleep(0.2)
            self.find_and_click(self.__CANCEL_BUTTON)
            return HistoryOrder(self.driver)

    def get_error_message(self, model):
        with allure.step(f"获取{model}输入框的错误信息"):
            error_message = self.find(By.XPATH,
                                      f"//*[text() = '{model}']/../div/*[@class = 'el-form-item__error']").text
        return error_message

    def get_input_value(self, model):
        with allure.step(f"获取{model}输入框已输入的值"):
            input_value = self.find(By.XPATH, f"//*[text() = '{model}']/../div/div/div/input").get_attribute("value")
        return input_value

    #

    def add_co_borrower(self, idcard_front=None, idcard_back=None, name=None, gender=None, ssn=None,
                        idcard_address=None, idcard_start_date=None,
                        idcard_end_date=None, phone=None, address=None, detail_address=None, relationship=None):
        with allure.step("点击新增共借人"):
            self.find_and_click(self.__ADD_CO_BORROWER)
        if (idcard_front and idcard_back) is not None:
            ele = self.finds(self.__CORPORATE_IDCARD_UPLOAD)
            if idcard_front is not None:
                with allure.step("上传身份证正面"):
                    ele[0].send_keys(idcard_front)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
            if idcard_back is not None:
                with allure.step("上传身份证反面"):
                    ele[1].send_keys(idcard_back)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
        if name is not None:
            with allure.step(f"输入共借人姓名{name}"):
                self.find(self.__CO_BORROWER_NAME).clear()
                self.find_and_send(self.__CO_BORROWER_NAME, text=name)
        if gender is not None:
            with allure.step(f"选择共借人性别为{gender}"):
                self.find_and_click(By.XPATH, f"//*[text()='添加共借人']/../..//*[text()='{gender}']/..")
        if ssn is not None:
            with allure.step(f"输入共借人证件号码{ssn}"):
                self.find(self.__CO_BORROWER_SSN).clear()
                self.find_and_send(self.__CO_BORROWER_SSN, text=ssn)
        if idcard_address is not None:
            with allure.step(f"输入共借人身份证地址{idcard_address}"):
                self.find(self.__CO_BORROWER_IDCARD_ADDRESS).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_ADDRESS, text=idcard_address)
        if idcard_start_date is not None:
            with allure.step(f"输入共借人证件有效期开始日期{idcard_start_date}"):
                self.find(self.__CO_BORROWER_IDCARD_START_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_START_DATE, text=idcard_start_date)
        if idcard_end_date is not None:
            with allure.step(f"输入共借人证件有效期结束日期{idcard_end_date}"):
                self.find(self.__CO_BORROWER_IDCARD_END_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_END_DATE, text=idcard_end_date)
        if phone is not None:
            with allure.step(f"输入共借人手机号{phone}"):
                self.find_and_send(self.__CO_BORROWER_PHONE, text=phone)
        if address is not None:
            with allure.step(f"选择共借人现居地{address}"):
                self.find_and_click(self.__CO_BORROWER_ADDRESS)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[0]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[1]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[2]}']")
        if detail_address is not None:
            with allure.step(f"输入详细地址{detail_address}"):
                self.find_and_send(self.__CO_BORROWER_DETAIL_ADDRESS, text=detail_address)
        if relationship is not None:
            with allure.step(f"选择共借人与主借人的关系{relationship}"):
                self.find_and_click(self.__CO_BORROWER_RELATIONSHIP)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{relationship}']")
        return AddHistoryOrder(self.driver)

    def add_guarantor(self, idcard_front=None, idcard_back=None, name=None, gender=None, ssn=None,
                      idcard_address=None, idcard_start_date=None, idcard_end_date=None, phone=None, address=None,
                      detail_address=None, relationship=None):
        with allure.step("点击新增担保人"):
            self.find_and_click(self.__ADD_GUARANTOR)
        if (idcard_front and idcard_back) is not None:
            ele = self.finds(self.__CORPORATE_IDCARD_UPLOAD)
            if idcard_front is not None:
                with allure.step("上传身份证正面"):
                    ele[0].send_keys(idcard_front)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
            if idcard_back is not None:
                with allure.step("上传身份证反面"):
                    ele[1].send_keys(idcard_back)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
        if name is not None:
            with allure.step(f"输入担保人姓名{name}"):
                self.find(self.__CO_BORROWER_NAME).clear()
                self.find_and_send(self.__CO_BORROWER_NAME, text=name)
        if gender is not None:
            with allure.step(f"选择担保人性别为{gender}"):
                self.find_and_click(By.XPATH, f"//*[text()='添加担保人']/../..//*[text()='{gender}']/..")
        if ssn is not None:
            with allure.step(f"输入担保人证件号码{ssn}"):
                self.find(self.__CO_BORROWER_SSN).clear()
                self.find_and_send(self.__CO_BORROWER_SSN, text=ssn)
        if idcard_address is not None:
            with allure.step(f"输入担保人身份证地址{idcard_address}"):
                self.find(self.__CO_BORROWER_IDCARD_ADDRESS).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_ADDRESS, text=idcard_address)
        if idcard_start_date is not None:
            with allure.step(f"输入担保人证件有效期开始日期{idcard_start_date}"):
                self.find(self.__CO_BORROWER_IDCARD_START_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_START_DATE, text=idcard_start_date)
        if idcard_end_date is not None:
            with allure.step(f"输入担保人证件有效期结束日期{idcard_end_date}"):
                self.find(self.__CO_BORROWER_IDCARD_END_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_END_DATE, text=idcard_end_date)
        if phone is not None:
            with allure.step(f"输入担保人手机号{phone}"):
                self.find_and_send(self.__CO_BORROWER_PHONE, text=phone)
        if address is not None:
            with allure.step(f"选择担保人现居地{address}"):
                self.find_and_click(self.__CO_BORROWER_ADDRESS)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[0]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[1]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[2]}']")
        if detail_address is not None:
            with allure.step(f"输入详细地址{detail_address}"):
                self.find_and_send(self.__CO_BORROWER_DETAIL_ADDRESS, text=detail_address)
        if relationship is not None:
            with allure.step(f"选择担保人与主借人的关系{relationship}"):
                self.find_and_click(self.__CO_BORROWER_RELATIONSHIP)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{relationship}']")
        return AddHistoryOrder(self.driver)

    def add_co_borrower_upload(self, file_type, file_path):
        with allure.step('点击添加文件'):
            self.find_and_click(self.__CO_BORROWER_ADD_FILE)
        with allure.step('点击选择附件分类'):
            self.find_and_click(self.__CO_BORROWER_FILE_TYPE)
            self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{file_type}']")
        with allure.step('上传文件'):
            self.find_and_click(self.__CO_BORROWER_SELECT_FILE)
            self.find_and_send(self.__CO_BORROWER_UPLOAD, text=file_path)
            self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=20)
        return AddHistoryOrder(self.driver)

    def get_co_borrower_upload_file_name(self):
        with allure.step("获取上传文件名称"):
            name = self.find(self.__CO_BORROWER_FILE_NAME).text
        return name

    def download_co_borrower_upload_file(self):
        with allure.step("点击下载文件"):
            self.find_and_click(self.__CO_BORROWER_DOWNLOAD_FILE)
        return AddHistoryOrder(self.driver)

    def delete_co_borrower_upload_file(self):
        with allure.step("点击删除"):
            self.find_and_click(self.__CO_BORROWER_DELETE_FILE)
        try:
            self.find(self.__CO_BORROWER_DELETE_FILE)
            return False
        except NoSuchElementException:
            return True

    def edit_co_borrower(self, idcard_front=None, idcard_back=None, name=None, gender=None, ssn=None,
                         idcard_address=None, idcard_start_date=None, idcard_end_date=None, phone=None, address=None,
                         detail_address=None, relationship=None):
        with allure.step("点击编辑"):
            self.find_and_click(self.__CO_BORROWER_EDIT)
        if (idcard_front and idcard_back) is not None:
            ele = self.finds(self.__CORPORATE_IDCARD_UPLOAD)
            if idcard_front is not None:
                with allure.step("上传身份证正面"):
                    ele[0].send_keys(idcard_front)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
            if idcard_back is not None:
                with allure.step("上传身份证反面"):
                    ele[1].send_keys(idcard_back)
                    self.wait(EC.staleness_of(self.find(self.__ElE_MASK)), time_out=10)
                    time.sleep(0.2)
        if name is not None:
            with allure.step(f"输入担保人姓名{name}"):
                self.find(self.__CO_BORROWER_NAME).clear()
                self.find_and_send(self.__CO_BORROWER_NAME, text=name)
        if gender is not None:
            with allure.step(f"选择担保人性别为{gender}"):
                self.find_and_click(By.XPATH, f"//*[text()='添加担保人']/../..//*[text()='{gender}']/..")
        if ssn is not None:
            with allure.step(f"输入担保人证件号码{ssn}"):
                self.find(self.__CO_BORROWER_SSN).clear()
                self.find_and_send(self.__CO_BORROWER_SSN, text=ssn)
        if idcard_address is not None:
            with allure.step(f"输入担保人身份证地址{idcard_address}"):
                self.find(self.__CO_BORROWER_IDCARD_ADDRESS).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_ADDRESS, text=idcard_address)
        if idcard_start_date is not None:
            with allure.step(f"输入担保人证件有效期开始日期{idcard_start_date}"):
                self.find(self.__CO_BORROWER_IDCARD_START_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_START_DATE, text=idcard_start_date)
        if idcard_end_date is not None:
            with allure.step(f"输入担保人证件有效期结束日期{idcard_end_date}"):
                self.find(self.__CO_BORROWER_IDCARD_END_DATE).clear()
                self.find_and_send(self.__CO_BORROWER_IDCARD_END_DATE, text=idcard_end_date)
        if phone is not None:
            self.find(self.__CO_BORROWER_PHONE).clear()
            with allure.step(f"输入担保人手机号{phone}"):
                self.find_and_send(self.__CO_BORROWER_PHONE, text=phone)
        if address is not None:
            self.find(self.__CO_BORROWER_ADDRESS).clear()
            with allure.step(f"选择担保人现居地{address}"):
                self.find_and_click(self.__CO_BORROWER_ADDRESS)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[0]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[1]}']")
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{address[2]}']")
        if detail_address is not None:
            with allure.step(f"输入详细地址{detail_address}"):
                self.find(self.__CO_BORROWER_DETAIL_ADDRESS).clear()
                self.find_and_send(self.__CO_BORROWER_DETAIL_ADDRESS, text=detail_address)
        if relationship is not None:
            with allure.step(f"选择担保人与主借人的关系{relationship}"):
                self.find_and_click(self.__CO_BORROWER_RELATIONSHIP)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{relationship}']")
        return AddHistoryOrder(self.driver)

    def delete_co_borrower(self):
        with allure.step("点击删除"):
            self.find_and_click(self.__CO_BORROWER_DELETE)
            self.find_and_click(self.__DELETE_SECOND_OK)
        try:
            self.wait(EC.staleness_of(self.find(self.__FIRST_INFO_NAME)))
            return True
        except:
            return False

    def add_collateral_info(self, property_ownership_certificate=None, property_owner_idcard=None,
                            property_owner_name=None, property_owner_phone=None, property_owner_address=None,
                            ownership_certificate=None, property_address=None, building_area=None, evaluate_value=None,
                            marital_status=None, belong_region=None, power_value=None, mortgage_rate=None):
        with allure.step("点击新增抵押物"):
            self.find_and_click(self.__ADD_COLLATERAL)
        if property_ownership_certificate is not None:
            with allure.step("上传房产证"):
                self.find_and_send(self.__PROPERTY_OWNERSHIP_CERTIFICATE, text=property_ownership_certificate)
        if property_owner_idcard is not None:
            with allure.step(f"输入产权人证件号码{property_owner_idcard}"):
                self.find_and_send(self.__PROPERTY_OWNER_IDCARD, text=property_owner_idcard)
        if property_owner_name is not None:
            with allure.step(f"输入产权人姓名{property_owner_name}"):
                self.find_and_send(self.__PROPERTY_OWNER_NAME, text=property_owner_name)
        if property_owner_phone is not None:
            with allure.step(f"输入产权人手机号{property_owner_phone}"):
                self.find_and_send(self.__PROPERTY_OWNER_PHONE, text=property_owner_phone)
        if property_owner_address is not None:
            with allure.step(f"输入产权人地址{property_owner_address}"):
                self.find_and_send(self.__PROPERTY_OWNER_ADDRESS, text=property_owner_address)
        if ownership_certificate is not None:
            with allure.step(f"输入权属证书编号{ownership_certificate}"):
                self.find_and_send(self.__OWNERSHIP_CERTIFICATE, text=ownership_certificate)
        if property_address is not None:
            with allure.step(f"输入抵押物地址{property_address}"):
                self.find_and_send(self.__PROPERTY_ADDRESS, text=property_address)
        if building_area is not None:
            with allure.step(f"输入建筑面积{building_area}"):
                self.find_and_send(self.__BUILDING_AREA, text=building_area)
        if evaluate_value is not None:
            with allure.step(f"输入评估价值{evaluate_value}"):
                self.find_and_send(self.__EVALUATE_VALUE, text=evaluate_value)
        if marital_status is not None:
            with allure.step(f"选择婚姻状况{marital_status}"):
                self.find_and_click(self.__MARITAL_STATUS)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{marital_status}']")
        if belong_region is not None:
            with allure.step(f"输入所属区域{belong_region}"):
                self.find_and_send(self.__BELONG_REGION, text=belong_region)
        if power_value is not None:
            with allure.step(f"输入权力价值{power_value}"):
                self.find_and_send(self.__POWER_VALUE, text=power_value)
        if mortgage_rate is not None:
            with allure.step(f"输入抵押率{mortgage_rate}"):
                self.find_and_send(self.__MORTGAGE_RATE, text=mortgage_rate)
        return AddHistoryOrder(self.driver)

    def click_add_info_ok_button(self):
        with allure.step("点击确认,提交抵押物信息"):
            self.find_and_click(self.__ADD_COLLATERAL_OK)
        return AddHistoryOrder(self.driver)

    #
    def get_collateral_first_info_name(self):
        with allure.step("获取抵押物信息第一行产权人姓名"):
            time.sleep(0.2)
            collateral_first_info_name = self.find(self.__COLLATERAL_FIRST_INFO_NAME).text
        return collateral_first_info_name

    def get_first_info_name(self):
        with allure.step("获取第一行姓名"):
            time.sleep(0.2)
            first_info_name = self.find(self.__FIRST_INFO_NAME).text
        return first_info_name

    def delete_first_collateral_info(self):
        with allure.step("删除第一条抵押物信息"):
            self.find_and_click(self.__DELETE_FIRST_COLLATERAL)
            self.find_and_click(self.__DELETE_SECOND_OK)
        try:
            self.wait(EC.visibility_of_element_located(self.__DELETE_SUCCESSFULLY_MESSAGE))
            return True
        except:
            return False

    def modify_collateral_info(self, property_ownership_certificate=None, property_owner_idcard=None,
                               property_owner_name=None, property_owner_phone=None, property_owner_address=None,
                               ownership_certificate=None, property_address=None, building_area=None,
                               evaluate_value=None,
                               marital_status=None, belong_region=None, power_value=None, mortgage_rate=None):
        with allure.step("点击修改抵押物"):
            self.find_and_click(self.__MODIFY_COLLATERAL)
        if property_ownership_certificate is not None:
            with allure.step("上传房产证"):
                self.find_and_send(self.__PROPERTY_OWNERSHIP_CERTIFICATE, text=property_ownership_certificate)
        if property_owner_idcard is not None:
            with allure.step(f"输入产权人证件号码{property_owner_idcard}"):
                self.find(self.__PROPERTY_OWNER_IDCARD).clear()
                self.find_and_send(self.__PROPERTY_OWNER_IDCARD, text=property_owner_idcard)
        if property_owner_name is not None:
            with allure.step(f"输入产权人姓名{property_owner_name}"):
                self.find(self.__PROPERTY_OWNER_NAME).clear()
                self.find_and_send(self.__PROPERTY_OWNER_NAME, text=property_owner_name)
        if property_owner_phone is not None:
            with allure.step(f"输入产权人手机号{property_owner_phone}"):
                self.find(self.__PROPERTY_OWNER_PHONE).clear()
                self.find_and_send(self.__PROPERTY_OWNER_PHONE, text=property_owner_phone)
        if property_owner_address is not None:
            self.find(self.__PROPERTY_OWNER_ADDRESS).clear()
            with allure.step(f"输入产权人地址{property_owner_address}"):
                self.find_and_send(self.__PROPERTY_OWNER_ADDRESS, text=property_owner_address)
        if ownership_certificate is not None:
            with allure.step(f"输入权属证书编号{ownership_certificate}"):
                self.find(self.__OWNERSHIP_CERTIFICATE).clear()
                self.find_and_send(self.__OWNERSHIP_CERTIFICATE, text=ownership_certificate)
        if property_address is not None:
            with allure.step(f"输入抵押物地址{property_address}"):
                self.find(self.__PROPERTY_ADDRESS).clear()
                self.find_and_send(self.__PROPERTY_ADDRESS, text=property_address)
        if building_area is not None:
            with allure.step(f"输入建筑面积{building_area}"):
                self.find(self.__BUILDING_AREA).clear()
                self.find_and_send(self.__BUILDING_AREA, text=building_area)
        if evaluate_value is not None:
            with allure.step(f"输入评估价值{evaluate_value}"):
                self.find(self.__EVALUATE_VALUE).clear()
                self.find_and_send(self.__EVALUATE_VALUE, text=evaluate_value)
        if marital_status is not None:
            with allure.step(f"选择婚姻状况{marital_status}"):
                self.find_and_click(self.__MARITAL_STATUS)
                self.find_and_click(By.XPATH, f"//div[@aria-hidden = 'false']//span[text() = '{marital_status}']")
        if belong_region is not None:
            with allure.step(f"输入所属区域{belong_region}"):
                self.find(self.__BELONG_REGION).clear()
                self.find_and_send(self.__BELONG_REGION, text=belong_region)
        if power_value is not None:
            with allure.step(f"输入权力价值{power_value}"):
                self.find(self.__POWER_VALUE).clear()
                self.find_and_send(self.__POWER_VALUE, text=power_value)
        if mortgage_rate is not None:
            with allure.step(f"输入抵押率{mortgage_rate}"):
                self.find(self.__MORTGAGE_RATE).clear()
                self.find_and_send(self.__MORTGAGE_RATE, text=mortgage_rate)
        return AddHistoryOrder(self.driver)

    def get_message(self):
        with allure.step("获取提示信息"):
            message = self.find(self.__MESSAGE).text
        return message
